
package com.vipheyue.bigcompany.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.vipheyue.bigcompany.R;

/**
 * 缓存类
 *
 * @author heyue
 */
public class ViewHolder {
    private final SparseArray<View> mViews;//可以存放很多个view
    private static int mPosition;
    private View mConvertView;
    private Context context;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
        this.context = context;
    }

    /**
     * 拿到一个ViewHolder对象,入口方法
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        }
        mPosition = position;
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 入口方法
     * 通过控件的Id获取对于的控件，如果没有则加入views
     * 相当于 nameView=(TextView) convertView.findViewById(R.id.name);
     * CacheView cache=new CacheView();
     * cache.nameView=nameView;
     * convertView.setTag(cache);
     *
     * @param viewId
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }
    public ViewHolder setTextStr(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }
    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }
    public ViewHolder setImageDrawble(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);

        return this;
    }


    /**
     * 为ImageView设置图片 xUtils工具类
     */
    public ViewHolder setImageURI(int viewId, String uri) {
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadingImage(R.drawable.common_add_bank_card);//默认背景图片
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
        ImageView view = getView(viewId);
        bitmapUtils.display(view, uri);
        return this;
    }


    public int getPosition() {
        return mPosition;
    }

}
